package com.fnzx.controller;

import java.util.Arrays;
import java.util.Map;

import com.fnzx.entity.RotationChartEntity;
import com.fnzx.service.RotationChartService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.R;
import com.fnzx.util.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-10-07 10:54:12z
 */
@RestController
@RequestMapping("iot/rotationchart")
@Api(tags ="轮播图")
public class RotationChartController {
    @Autowired
    private RotationChartService rotationChartService;

    /**
     * 列表
     */
    @ApiOperation(value = "后台和前台-分页查询带排序",notes = "查询出所有轮播图(page->当前页面,limit->每页记录数)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",paramType = "query",required = true,dataType = "String"),
            @ApiImplicitParam(name = "limit",value = "每页记录数",paramType = "query",required = true,dataType = "String")
    })
    @GetMapping("/list")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = rotationChartService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "后台页面-id查询",notes = "通过id查询出轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "轮播图id",paramType = "path",required = true,dataType = "String"),
    })
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        RotationChartEntity relation = rotationChartService.getById(id);

        return R.ok().put("rotationChart", relation);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "后台页面-添加", notes = "添加轮播图")
    @PostMapping("/save")
    public R save(@RequestBody RotationChartEntity relation,
                  @RequestHeader String token){
        rotationChartService.save(relation);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "后台页面-修改", notes = "修改轮播图")
        @PostMapping("/update")
        public R update(@RequestBody RotationChartEntity rotationChartEntity,
                @RequestHeader String token){
            Long id = rotationChartEntity.getId();
            String imageUrl = rotationChartService.getById(id).getImageUrl();
            if (!rotationChartEntity.getImageUrl().equals(imageUrl)) {
                OssUtils.delete(imageUrl);
            }
            rotationChartService.updateById(rotationChartEntity);

            return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "后台页面-删除", notes = "删除轮播图，可以同时删除多个")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids,
                    @RequestHeader String token){
        for (Long id : ids) {
            String imageUrl = rotationChartService.getById(id).getImageUrl();
            OssUtils.delete(imageUrl);
        }
        rotationChartService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
